import QtQuick 2.9
import QtQuick.Window 2.2
import BowenView 1.0

import SerialDataitem 1.0
import ComDataItem 1.0
import ConfigFileitem 1.0

Window {
    visible: true
    width: 1280
    height: 800
    color: "#FFFFFF"
    title: qsTr("呼气分析仪")
    flags: Qt.WindowStaysOnTopHint

    property bool  isInit: false

    Rectangle{
        anchors.fill: parent
        focus: true
        Keys.enabled: true
        Keys.onEscapePressed: Qt.quit();
    }

    HomePage{
        id:homePage
        x:0
        y:0
        width: parent.width
        height: parent.height

        onSigExhaleClick: {
            contenViews.isRecvDataStatus = true
            exhaleBlow.iniNumbesInfrs = false
            exhaleWait.iniNumbesInfrs = false
            exhaleBlow.openFunction();
            exhaleBlow.visible = true

        }
        //        onSigStandardClick: {
        //            standardBlow.visible = true
        //        }
    }


    ExhaleBlow{
        id:exhaleBlow
        x:0
        y:0
        width: parent.width
        height: parent.height
        visible: false
        onSigCancelClick: {
            contenViews.isRecvDataStatus = false
            bowenView.clearValue();
            console.log("exhaleBlow.onSigCancelClick")
            exhaleBlow.iniNumbesInfrs = false
            exhaleWait.iniNumbesInfrs = false
            exhaleBlow.closeFunction();
            exhaleBlow.visible = false
            homePage.handBackData();
            comDataItem.sendStatusInfor("3");
        }
        onSigSuccessClick: {
            console.log("exhaleBlow.onSigSuccessClick")
            exhaleBlow.iniNumbesInfrs = false
            exhaleWait.iniNumbesInfrs = false
            exhaleBlow.closeFunction();

            exhaleBlow.visible = false

            exhaleWait.openFunction();
            exhaleWait.visible = true
        }
    }

    ExhaleWait{
        id:exhaleWait
        x:0
        y:0
        width: parent.width
        height: parent.height
        visible: false
        onSigCancelClick: {
            contenViews.isRecvDataStatus = false
            bowenView.clearValue();
            exhaleBlow.iniNumbesInfrs = false
            exhaleWait.iniNumbesInfrs = false
            exhaleWait.closeFunction();

            exhaleWait.visible = false
            homePage.handBackData();
            comDataItem.sendStatusInfor("3");
        }
        onSigSuccessClick: {
            contenViews.isRecvDataStatus = false
            exhaleBlow.iniNumbesInfrs = false
            exhaleWait.iniNumbesInfrs = false
            exhaleWait.visible = false
            exhaleWait.closeFunction();

            exhaleResult.visible = true;

            var tempnums = Math.abs(exhaleWait.currentPpm - exhaleBlow.currentAirInfors * exhaleResult.currentFrequencys);
            comDataItem.sendEndInfor( tempnums+"");
        }
    }

    //    StandardBlow{
    //        id:standardBlow
    //        x:0
    //        y:0
    //        width: parent.width
    //        height: parent.height
    //        visible: false
    //        onSigCancelClick: {
    //            standardBlow.visible = false
    //        }
    //        onSigSuccessClick: {
    //            standardBlow.visible = false
    //            exhaleResult.currentPotencys = standarWait.currentMaxPpm

    //            standarWait.visible = true
    //        }
    //    }

    //    StandarWait{
    //        id:standarWait
    //        x:0
    //        y:0
    //        width: parent.width
    //        height: parent.height
    //        visible: false
    //        onSigCancelClick: {
    //            standarWait.visible = false
    //        }
    //        onSigSuccessClick: {
    //            standarWait.visible = false
    //            exhaleResult.visible = true
    //        }
    //    }

    ExhaleResult{
        id:exhaleResult
        x:0
        y:0
        width: parent.width
        height: parent.height
        currentAirInfors:exhaleBlow.currentAirInfors
        currentPpm:exhaleWait.currentPpm
        visible: false
        onSigCancelClick: {
            exhaleResult.visible = false;
            bowenView.clearValue();
        }
        onSigCancelLimitClick: {
            homePage. handLimtData()
            //   exhaleResult.visible = false;
            //  bowenView.clearValue();
        }
    }

    //bo xing
    Item {

        id:contenViews

        anchors.left: homePage.left
        anchors.top: homePage.top
        anchors.bottom: homePage.bottom
        anchors.right: homePage.right
        anchors.leftMargin: 30
        anchors.topMargin: 176
        anchors.rightMargin: 30
        anchors.bottomMargin: 130


        visible: true
        property bool isRecvDataStatus: false
        onIsRecvDataStatusChanged: {

            if(isRecvDataStatus){
                console.log("onIsRecvDataStatusChanged");
                bowenView.clearValue();
            }

        }
        Rectangle{
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.leftMargin: 21
            anchors.topMargin: 40
            width: 10
            height: 40
            radius: 5
            color: "#24B9B7"
        }
        Text {
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.leftMargin: 51
            anchors.topMargin: 40
            font.pixelSize: 30
            font.bold: true
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignLeft
            color:  "#303030"
            height: 40
            text: qsTr("波形")
        }
        Rectangle{
            id:contenInforViews
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.rightMargin: 0
            anchors.bottom: parent.bottom
            anchors.leftMargin: 60
            anchors.topMargin: 160
            anchors.bottomMargin: 70
            color: "#00000000"

            BowenView{
                id:bowenView
                width: contenInforViews.width
                height:contenInforViews.height
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            //            Text {
            //                anchors.leftMargin: 5
            //                anchors.left: bowenView.left
            //                anchors.bottom: parent.bottom
            //                color: "#999999"

            //                font.pixelSize: 12
            //                text: qsTr("0")
            //            }
            Text {
                anchors.leftMargin:0
                anchors.left: bowenView.left
                anchors.top:  parent.top
                anchors.topMargin: -12
                color: "#303030"

                font.pixelSize: 12
                text: bowenView.getMaxValue();
            }
        }
    }

    SerialDataitem{
        id:serialDataitem
        onSigSendPotency: {
            if(contenViews.isRecvDataStatus){
                console.log("isRecvDataStatus potency=="+potency);
                comDataItem.sendSerielInfor(potency * 1.0 +"");
                bowenView.insertValue(potency * 1.0);
            }
            console.log("isRecvDataStatus potency=11="+potency);
        }
    }

    PowerRecy{
        id:powerRecy
        anchors.right: homePage.right
        anchors.top: homePage.top
        anchors.topMargin: 20
        anchors.rightMargin: 34
        currentPecrent:homePage.currtentMacheAdc
        visible: configFileitem.getPowers()>=1?true:false

    }
    Timer{
        id:inforTimer
        running: false
        interval: 10
        repeat: false
        onTriggered: {
            inforTimer.stop();
            showFullScreen();
        }
    }

    Component.onCompleted: {
        if(!isInit){
            isInit = true;
            inforTimer.start();
        }

    }


    ComDataItem{
        id:comDataItem
        onSigCheckStatus: {
            if(statuses == 0){
                if( contenViews.isRecvDataStatus ){
                    comDataItem.sendStatusInfor("1");
                }else{
                    if( homePage.currentHandlStatus){
                        comDataItem.sendStatusInfor("0");

                    }else{
                        comDataItem.sendStatusInfor("4");

                    }

                }
                return;
            }
            if(statuses == 1){
                if( !contenViews.isRecvDataStatus ){
                    if( homePage.currentHandlStatus){
                        if(exhaleResult.visible){
                            exhaleResult.handSuccessBtn();
                        }
                        homePage.handSuccessBtn();
                    }else{
                        comDataItem.sendStatusInfor("4");
                        return;
                    }

                }
                comDataItem.sendStatusInfor("1");
                return;
            }
            if(statuses == 2 || statuses == 3 ){
                if( !contenViews.isRecvDataStatus ){
                    if(exhaleBlow.visible){
                        exhaleBlow.handlCancelClick();
                    }
                    if(exhaleWait.visible){
                        exhaleBlow.handlCancelClick();
                    }
                    comDataItem.sendStatusInfor("4");

                    return;
                }

                comDataItem.sendStatusInfor("0");

                return;
            }

        }
    }


    ConfigFileitem{
        id:configFileitem
    }


}
